package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.ActiveDeal;
import com.bigfreakindeals.Models.DiscussionComment;
import com.bigfreakindeals.Models.UserProfile;

public class GetCommentsHandler  {
	
	static final String STATUS = "status";
	
	static final String SESSION_TOKEN = "sessionToken";

	static final  String ERROR = "error";
	
	static final  String USER_NAME = "user_name";
	static final  String AVATAR = "user_avatar";
	static final  String MESSAGE = "message";
	static final  String POST_DATE = "post_date";
	static final  String FIRSTNAME = "user_first_name";
	static final  String LAST_NAME = "user_last_name";
	
	String feedUrl;
	 boolean discussBool;

	private static final String SOAP_ACTION = "1";
    private static final String METHOD_NAME = "bigfreakindeals";
    private static final String NAMESPACE = "1";
    
    
	public GetCommentsHandler(String feedUrl) {
		this.feedUrl = feedUrl;
		parse();
	}
	
	 public void parse() {

	     try {
	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.setOutputSoapObject(request);
		
		    HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

			try{
				androidHttpTransport.call(SOAP_ACTION, envelope);
			}catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
			SoapObject response = (SoapObject) envelope.getResponse();

			String discuss = null;
			
			 if(response.getProperty(STATUS)!=null)
				 discuss  = response.getProperty(STATUS).toString();
		   		 if (discuss !=null && discuss.length() >0){
	       			 if (discuss.contentEquals("false")){
	       				discussBool = false;
	       			 }else{
	       				discussBool = true;
	       			 }
	       		 }
						
			if(discussBool){
				Vector comments = (Vector)response.getProperty("deal_comments");
				
				
				ApplicationEx.getInstance().commentList = null;
				for (int count = 0;count <= comments.size() -1; count++){
					
					SoapObject post = (SoapObject)comments.get(count);
					DiscussionComment postItem = new DiscussionComment();
					String commentsName = null;
					String commentsAvatar= null;
					String commentsMessage = null;
					String commentsPostDate = null;
					
					/*if (post.getProperty(USER_NAME) != null){
						commentsName = post.getProperty(USER_NAME).toString();
						postItem.setUserName(commentsName);
						
					}*/
					
					if (post.getProperty(FIRSTNAME) != null){
						commentsName = post.getProperty(FIRSTNAME).toString();
						postItem.setUserName(commentsName);
						
					}
					if (post.getProperty(LAST_NAME) != null){
						commentsName = post.getProperty(LAST_NAME).toString();
						postItem.setUserName(postItem.getUserName() +" "+commentsName);
						
					}
					
					
					if (post.getProperty(AVATAR) != null){
						commentsAvatar = post.getProperty(AVATAR).toString();
						postItem.setAvatar(commentsAvatar);
					}
					
					if (post.getProperty(MESSAGE) != null){
						commentsMessage = post.getProperty(MESSAGE).toString();
						postItem.setMessage(commentsMessage);
						
					}
					
					if (post.getProperty(POST_DATE) != null){
						commentsPostDate = post.getProperty(POST_DATE).toString();
						postItem.setPostDate(commentsPostDate);
					}
					if (ApplicationEx.getInstance().commentList == null)				
						ApplicationEx.getInstance().commentList = new ArrayList<DiscussionComment>();					
					
					ApplicationEx.getInstance().commentList.add(postItem);
					
				}
				
				Intent intent = new Intent(Constant.EVENTS_GETDISCUSSLIST);
				ApplicationEx.getInstance().sendBroadcast(intent);


				
			}else{
				
				String error = response.getProperty(ERROR).toString();
				Intent intent = new Intent(Constant.EVENTS_GETDISCUSSLISTERROR);
				intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE,error);
				ApplicationEx.getInstance().sendBroadcast(intent);
				return;
				
			}
			
	     } catch (IOException e1) {
				// TODO Auto-generated catch block
	    		
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
		
			} catch (Exception e1) {
				
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	     }
}